<?php


namespace app\index\model;


use app\common\controller\Basecontroller;
use think\Db;
use think\Model;
/*
 * insertBoss 插入老板账号
 * */

class User extends Model
{
    /**
     * 用于注册时，添加用户信息
     * ywh开发
     *
     * @param $data
     * @return bool
     */
    public function push($data){
        return  $this->save($data);
    }

    /**
     * 利用手机查询用户id
     * ywh开发
     * @param $phone
     * @return User
     */
    public function selectUserID($phone){
        return $this->where('phone','=',$phone)->field('user_id')->find();
    }

    /**
     *用于用户修改个人基本信息
     * ywh开发
     *
     * @param $phone
     * @param $data
     * @return User
     */
    public function humanPush($phone,$data){
        return  $this->where('phone','=',$phone)->update($data);
    }

    /**
     * 用于用户修改手机号
     * ywh开发
     *
     * @param $userID
     * @param $phone
     * @return User
     */
    public function upPhone($userID,$phone){
        return $this->where('user_id','=',$userID)->update($phone);
    }

    /**
     * 用于获取用户所有信息
     * ywh开发
     *
     * @param $filed
     * @param $option
     * @param $data
     * @return User[]|\think\Collection
     */
    public function getAllAttr($filed,$option,$data){
        return  $this->where($filed,$option,$data)->select();
    }

    /**
     * 用于获取其中一条用户的信息
     * ywh开发
     *
     * @param $filed
     * @param $option
     * @param $data
     * @return User
     */
    public function getOneAttr($filed,$option,$data){
        return  $this->where($filed,$option,$data)->find();
    }

    /**
     * 获取公司id，判断当前用户，查询相应字段
     * @copyright hyw
     * @param $userID
     * @return User
     */
    public function getComiDAttr($userID) {
        return $this
            ->where('user_id','=',$userID)
            ->field('company_id')
            ->find();
    }

    /**
     * 获取人事信息，判断当前登录用户，连接职位表，外键用户id，查询相应字段，返回数据
     * @param $phone
     * @return User[]|\think\Collection
     */
    public function getMatterAttr($companyId) {
        return $this
            ->alias('u')
            ->where('u.company_id','=',$companyId)
            ->join(['official' => 'o'],'o.user_id = u.user_id')
            ->where('position','=',1)
            ->where('u.is_delete','=',0)
            ->field(['u.username','u.header','o.official'])
            ->select();
    }

    /**
     * @param $insertBoss
     * @return void
     * ljj
     * 插入老板账号
     */
    public function insertBoss($insertBoss)
    {
        $this->save($insertBoss);
    }

    /**
     * @param $userId
     * @param $companyId
     * @return void
     * 获取老板id
     */
    public function getUser($userId,$companyId)
    {
        $this->where('user_id','=',$userId)->update(['position'=>1,'company_id'=>$companyId]);
    }

    /**
     * 获取人事信息
     * @copyright hyw
     * @param $user_id
     * @return User[]|\think\Collection
     */
    public function getMatterLogoAttr($user_id)
    {
        return $this
            ->where('user_id', '=', $user_id)
            ->field(['header', 'username'])
            ->select();
    }

    /**
     * @param $user
     * @return User
     * 公司中心 ljj
     */
    public function getUserCompanyAttr($user)
    {
        return $this->where('user_id','=',$user)->field('company_id')->find();
    }

    public function UserHeader($userId)
    {
        return $this->where('user_id','=',$userId)->field('header')->find();
    }
}